package com.htht.smartschool.service.department;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.htht.smartschool.common.service.BaseService;
import com.htht.smartschool.model.department.TDepartment;

public class DepartmentService extends BaseService {
	public boolean addDepartment(TDepartment tdepartment) {
		String sql = "insert into t_department (id,departmentcode,department,office,schoolcode) values "
				+ "(null,:departmentcode,:department,:office,:schoolcode)";
		int res = getDao().executeSQL(sql,tdepartment);
		if(res == -1){
			return false;
		}else{
			return true;
		}
	}
	@SuppressWarnings("unchecked")
	public List<TDepartment> getDepartmentByPage(TDepartment tdepartment) {
		String sql = "select * from t_department where 1 = 1 and schoolcode = '"+tdepartment.getSchoolcode()+"'";
		String sql2 = "select count(*) from t_department where 1 = 1 and schoolcode = '"+tdepartment.getSchoolcode()+"'";
		List<Object[]> list = getDao().queryBySqlPage(sql, tdepartment.getCurrentpage(), tdepartment.getPagesize());
		int count = getDao().getCountBySqlPage(sql2);
		List<TDepartment> lis = new ArrayList<TDepartment>();
		for(int i = 0; i < list.size(); i++){
			Object[] obj = list.get(i);
			TDepartment td = new TDepartment();
			td.setDepartmentcode(String.valueOf(obj[1]));
			td.setDepartment(String.valueOf(obj[2]));
			td.setOffice(String.valueOf(obj[3]));
			td.setTotal((long) count);
			lis.add(td);
		}
		return lis;
	}
	
	public boolean updateDepartment(TDepartment tdepartment) {
		String sql = "update t_department set department = :department,office=:office where departmentcode = :departmentcode";
		int res = getDao().executeSQL(sql,tdepartment);
		if(res == -1){
			return false;
		}else{
			return true;
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map deleteDepartment(TDepartment tdepartment) {
		Map map = new HashMap();
		String departmentCodes = tdepartment.getDepartmentcodes();
		String tdSql = "";
		String[] list = departmentCodes.split(",");
		for(int i = 0; i < list.length; i++){
			int count = getCountByDepartmentcode(list[i]);
			if(count == 0){
				tdSql += "'"+list[i]+"',";
				map.put("data" + i, list[i]+"删除成功");
			}else{
				map.put("data" + i, list[i]+"存在教师删除失败");
			}
		}
		if(tdSql.length() > 0){
			tdSql = tdSql.substring(0,tdSql.length()-1);
			String sql = "delete from t_department where departmentcode in ("+tdSql+")";
			getDao().executeSQL(sql);
		}else{
			map.put("data", "该条记录不能被删除");
		}
		return map;
	}
	public int getCountByDepartmentcode(String departmentcode){
		String sql = "select count(*) from t_teacher_department where departmentcode = :departmentcode";
		TDepartment tdepartment = new TDepartment();
		tdepartment.setDepartmentcode(departmentcode);
		int count = getDao().getCountBySqlPage(sql,tdepartment);
		return count;
	}
	@SuppressWarnings("unchecked")
	public List<TDepartment> getAllDepartments(TDepartment tdepartment) {
		String sql ="SELECT DISTINCT td.department,GROUP_CONCAT(td.office ORDER BY td.departmentcode ASC) AS teachers,GROUP_CONCAT(td.departmentcode ORDER BY td.departmentcode ASC) AS departmentcodes FROM t_department td WHERE schoolcode = '"+tdepartment.getSchoolcode()+"' GROUP BY td.department";
		List<Object[]> list = getDao().queryBySQL(sql);
		List<TDepartment> result = new ArrayList<TDepartment>();
		for (Object[] obj : list) {
			TDepartment td = new TDepartment();
			td.setDepartment(String.valueOf(obj[0]));
			td.setOffice(String.valueOf(obj[1]));
			td.setDepartmentcode(String.valueOf(obj[2]));
			result.add(td);
		}
		return result;
	}
}
